﻿using Library.AppWeb;
using MPR.Poker.Model.SYS;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MPR.Poker.DALEntity.SYS
{
    public class DAL_TournamentStepTable
    {
        public DAL_TournamentStepTable()
        {
        }

        #region Insert/Update/Delete

        /// <summary>
        /// Thêm đặt bàn
        /// </summary>
        /// <param name="objTournamentStepTable"></param>
        /// <returns></returns>
        public ResultMessage Insert(SYS_TournamentStepTable objTournamentStepTable)
        {
            ResultMessage objResultMessage = new ResultMessage();
            //objResultMessage = CheckExist(objTournamentStepTable);
            //if (objResultMessage.IsError) return objResultMessage;

            objResultMessage = InsertNoCheck(objTournamentStepTable);

            return objResultMessage;
        }

        /// <summary>
        /// Kiểm Tra đặt bàn Đã Tồn Tại
        /// </summary>
        /// <param name="strIDSys"></param>
        /// <param name="strTournamentStepTableID"></param>
        /// <returns></returns>
        public ResultMessage CheckExist(SYS_TournamentStepTable objTournamentStepTable)
        {
            ResultMessage objResultMessage = new ResultMessage();
            try
            {
                //using (PokerLife db = new PokerLife())
                //{
                //    SYS_TournamentStepTable TournamentStepTable = db.SYS_TournamentStepTables.FirstOrDefault(i => i.IsDeleted == false & i.StepTableID == objTournamentStepTable.StepTableID & i.IDSys != objTournamentStepTable.IDSys);

                //    if (TournamentStepTable != null && TournamentStepTable.IDSys > 0)
                //    {
                //        objResultMessage = new ResultMessage(true, ResultMessage.ErrorTypes.No_Error, "đặt bàn đã tồn tại", string.Empty);
                //    }
                //}
            }
            catch (Exception objEx)
            {
                objResultMessage = new ResultMessage(true, ResultMessage.ErrorTypes.CheckData, "Lỗi kiểm tra tồn tại đặt bàn", objEx);
                DAL_Error.Insert(objResultMessage, 0, "DAL_TournamentStepTable --> CheckExist");
                return objResultMessage;
            }
            return objResultMessage;
        }

        /// <summary>
        /// Thêm mới đặt bàn chưa kiểm tra tồn tại
        /// </summary>
        /// <param name="objTournamentStepTable"></param>
        /// <returns></returns>
        public ResultMessage InsertNoCheck(SYS_TournamentStepTable objTournamentStepTable)
        {
            ResultMessage objResultMessage = new ResultMessage();
            SYS_TournamentStepTable TournamentStepTable = new SYS_TournamentStepTable();
            Library.DataAccess.IData objIData = Library.DataAccess.Data.CreateData(DAL_Globals.ConnectString);  
            try
            {
                              
                objIData.ConnectNoEncrypt();
                objIData.BeginTransaction();

                SqlParameter[] para = new SqlParameter[9];
                para[0] = new SqlParameter("@StepTableID", objTournamentStepTable.StepTableID);
                para[1] = new SqlParameter("@ImageUrl", objTournamentStepTable.ImageUrl);
                para[2] = new SqlParameter("@TournamentIDSys", objTournamentStepTable.TournamentIDSys);
                para[3] = new SqlParameter("@TournamentStepIDSys", objTournamentStepTable.TournamentStepIDSys);
                para[4] = new SqlParameter("@Islocked", objTournamentStepTable.Islocked);
                para[5] = new SqlParameter("@IsPaid", objTournamentStepTable.IsPaid);
                para[6] = new SqlParameter("@Note", objTournamentStepTable.Note);
                para[7] = new SqlParameter("@CurrentPlayerNumber", objTournamentStepTable.CurrentPlayerNumber);
                SqlParameter p = new SqlParameter();
                p.Direction = ParameterDirection.Output;
                p.ParameterName = "@IDSys";
                p.Value = 0;
                para[8] = p;

                objIData.CreateStoredProcedue("sp_SYS_TournamentStepTable_ADD");
                objIData.AddParameter(para);
                objIData.ExecProcedure();

                Int64 ID =Convert.ToInt64(para[8].Value);
                new DAL_TournamentStepTableMember().InsertNoCheck(objIData,ID , objTournamentStepTable.TournamentSTMList);
                
                objIData.CommitTransaction();

                DAL_Event.Insert("Thêm mới đặt bàn - " + objTournamentStepTable.StepTableID.ToString(), 0, ResultMessage.ErrorTypes.Insert.ToString());
            }
            catch (Exception objEx)
            {
                objIData.RollBackTransaction();
                objResultMessage = new ResultMessage(true, ResultMessage.ErrorTypes.Insert, "Lỗi thêm mới đặt bàn", objEx);
                DAL_Error.Insert(objResultMessage, 0, "DAL_TournamentStepTable --> Insert");
                return objResultMessage;
            }

            return new ResultMessage(false, ResultMessage.ErrorTypes.No_Error, "Thêm mới đặt bàn thành công", string.Empty);
        }

        /// <summary>
        /// Cập nhật đặt bàn
        /// </summary>
        /// <param name="objTournamentStepTable"></param>
        /// <returns></returns>
        public ResultMessage Update(SYS_TournamentStepTable objTournamentStepTable)
        {
            ResultMessage objResultMessage = new ResultMessage();
            objResultMessage = CheckExist(objTournamentStepTable);
            if (objResultMessage.IsError) return objResultMessage;

            objResultMessage = UpdateNoCheck(objTournamentStepTable);

            return objResultMessage;
        }

        /// <summary>
        /// Cập nhật đặt bàn chưa kiểm tra tồn Tại
        /// </summary>
        /// <param name="objTournamentStepTable"></param>
        /// <returns></returns>
        public ResultMessage UpdateNoCheck(SYS_TournamentStepTable objTournamentStepTable)
        {
            ResultMessage objResultMessage = new ResultMessage();
            try
            {
                Library.DataAccess.IData objIData = Library.DataAccess.Data.CreateData(DAL_Globals.ConnectString);
                objIData.ConnectNoEncrypt();
                SqlParameter[] para = new SqlParameter[9];
                para[0] = new SqlParameter("@IDSys", objTournamentStepTable.IDSys);
                para[1] = new SqlParameter("@StepTableID", objTournamentStepTable.StepTableID);
                para[2] = new SqlParameter("@ImageUrl", objTournamentStepTable.ImageUrl);
                para[3] = new SqlParameter("@TournamentIDSys", objTournamentStepTable.TournamentIDSys);
                para[4] = new SqlParameter("@TournamentStepIDSys", objTournamentStepTable.TournamentStepIDSys);
                para[5] = new SqlParameter("@Islocked", objTournamentStepTable.Islocked);
                para[6] = new SqlParameter("@IsPaid", objTournamentStepTable.IsPaid);
                para[7] = new SqlParameter("@Note", objTournamentStepTable.Note);
                para[8] = new SqlParameter("@CurrentPlayerNumber", objTournamentStepTable.CurrentPlayerNumber);

                objIData.CreateStoredProcedue("sp_SYS_TournamentStepTable_UPD");
                objIData.AddParameter(para);
                objIData.ExecProcedure();

                DAL_Event.Insert("Cập nhật đặt bàn - " + objTournamentStepTable.StepTableID.ToString(), 0, ResultMessage.ErrorTypes.Update.ToString());
            }
            catch (Exception objEx)
            {
                objResultMessage = new ResultMessage(true, ResultMessage.ErrorTypes.Update, "Lỗi cập nhật đặt bàn", objEx);
                DAL_Error.Insert(objResultMessage, 0, "DAL_TournamentStepTable --> Update");
                return objResultMessage;
            }

            return new ResultMessage(false, ResultMessage.ErrorTypes.No_Error, "Cập nhật đặt bàn thành công", string.Empty);
        }

        /// <summary>
        /// Xóa đặt bàn
        /// </summary>
        /// <param name="strIDSys"></param>
        /// <returns></returns>
        public ResultMessage Delete(SYS_TournamentStepTable objTournamentStepTable)
        {
            ResultMessage objResultMessage = new ResultMessage();
            objResultMessage = CheckRelationship(objTournamentStepTable);
            if (objResultMessage.IsError) return objResultMessage;

            objResultMessage = DeleteNoCheck(objTournamentStepTable);

            return objResultMessage;
        }

        /// <summary>
        /// Xóa đơn vị tiền tệ
        /// </summary>
        /// <param name="intTournamentStepTableIDSys">Mã hệ thống đơn vị tiền tệ</param>
        /// <param name="intDeletedUser">Mã người dùng</param>
        /// <returns></returns>
        public ResultMessage Delete(int intTournamentStepTableIDSys, int intDeletedUser)
        {
            ResultMessage objResultMessage = new ResultMessage();
            SYS_TournamentStepTable objTournamentStepTable = new SYS_TournamentStepTable();
            objTournamentStepTable.IDSys = intTournamentStepTableIDSys;

            objResultMessage = CheckRelationship(objTournamentStepTable);
            if (objResultMessage.IsError) return objResultMessage;

            objResultMessage = DeleteNoCheck(objTournamentStepTable);

            return objResultMessage;
        }

        /// <summary>
        /// Xóa đơn vị tiền tệ
        /// </summary>
        /// <param name="lstTournamentStepTableIDSys">Danh sách mã hệ thông đơn vị tiền tệ</param>
        /// <param name="intDeletedUser"></param>
        /// <returns></returns>
        public ResultMessage Delete(List<int> lstTournamentStepTableIDSys, int intDeletedUser)
        {
            ResultMessage objResultMessage = new ResultMessage();
            foreach (int intIDSys in lstTournamentStepTableIDSys)
            {
                SYS_TournamentStepTable objTournamentStepTable = new SYS_TournamentStepTable();
                objTournamentStepTable.IDSys = intIDSys;

                objResultMessage = CheckRelationship(objTournamentStepTable);
                if (objResultMessage.IsError) return objResultMessage;

                objResultMessage = DeleteNoCheck(objTournamentStepTable);
                if (objResultMessage.IsError) return objResultMessage;
            }
            return objResultMessage;
        }

        /// <summary>
        /// Kiểm tra dữ liệu liên quan
        /// </summary>
        /// <param name="strIDSys"></param>
        /// <returns></returns>
        public ResultMessage CheckRelationship(SYS_TournamentStepTable objTournamentStepTable)
        {
            ResultMessage objResultMessage = new ResultMessage();
            try
            {


            }
            catch (Exception objEx)
            {
                objResultMessage = new ResultMessage(true, ResultMessage.ErrorTypes.CheckData, "Lỗi kiểm tra dữ liệu liên quan đặt bàn", objEx);
                DAL_Error.Insert(objResultMessage, 0, "DAL_TournamentStepTable --> CheckRelationship");
                return objResultMessage;
            }
            return objResultMessage;
        }

        /// <summary>
        /// Xóa đặt bàn chưa kiểm tra dữ liệu liên quan
        /// </summary>
        /// <param name="strIDSys"></param>
        /// <returns></returns>
        public ResultMessage DeleteNoCheck(SYS_TournamentStepTable objTournamentStepTable)
        {
            ResultMessage objResultMessage = new ResultMessage();
            try
            {
                Library.DataAccess.IData objIData = Library.DataAccess.Data.CreateData(DAL_Globals.ConnectString);
                objIData.ConnectNoEncrypt();
                SqlParameter[] para = new SqlParameter[1];
                para[0] = new SqlParameter("@IDSys", objTournamentStepTable.IDSys);

                objIData.CreateStoredProcedue("sp_SYS_TournamentStepTable_DEL");
                objIData.AddParameter(para);
                objIData.ExecProcedure();

                DAL_Event.Insert("Xóa đặt bàn - " + objTournamentStepTable.StepTableID.ToString(), 0, ResultMessage.ErrorTypes.Insert.ToString());

            }
            catch (Exception objEx)
            {
                objResultMessage = new ResultMessage(true, ResultMessage.ErrorTypes.Delete, "Lỗi xóa đặt bàn", objEx);
                DAL_Error.Insert(objResultMessage, 0, "DAL_TournamentStepTable --> DeleteNoCheck");
                return objResultMessage;
            }
            return new ResultMessage(false, ResultMessage.ErrorTypes.No_Error, "Xóa đặt bàn thành công", string.Empty);
        }

        #endregion

        #region Load/Search/Select

        /// <summary>
        /// Lấy thông tin đặt bàn
        /// </summary>
        /// <param name="objTournamentStepTable"></param>
        /// <param name="strIDSys"></param>
        /// <returns></returns>
        public ResultMessage LoadInfo(ref SYS_TournamentStepTable objTournamentStepTable, Int64 intIDSys)
        {
            ResultMessage objResultMessage = new ResultMessage();
            try
            {
                Library.DataAccess.IData objIData = Library.DataAccess.Data.CreateData(DAL_Globals.ConnectString);
                objIData.ConnectNoEncrypt();
                objIData.CreateStoredProcedue("sp_SYS_TournamentStepTable_SEL");
                objIData.AddParameter("@IDSys", intIDSys);

                DataTable dt = objIData.GetDataTableProcedure();
                objTournamentStepTable = Library.AppWeb.ConvertData.ConvertToModel<SYS_TournamentStepTable>(dt);
            }
            catch (Exception objEx)
            {
                objResultMessage = new ResultMessage(true, ResultMessage.ErrorTypes.LoadInfo, "Lấy thông tin đặt bàn", objEx);
                DAL_Error.Insert(objResultMessage, 0, "DAL_TournamentStepTable --> LoadInfo");
                return objResultMessage;
            }
            return new ResultMessage(false, ResultMessage.ErrorTypes.No_Error, "Lấy thông tin đặt bàn thành công", string.Empty);
        }

        /// <summary>
        /// Lấy danh sách đặt bàn
        /// </summary>
        /// <param name="dtTournamentStepTable"></param>
        /// <returns></returns>
        public ResultMessage SearchData(ref DataTable dtTournamentStepTable)
        {
            ResultMessage objResultMessage = new ResultMessage();
            dtTournamentStepTable = new DataTable();

            try
            {
                using (PokerLife db = new PokerLife())
                {
                    dtTournamentStepTable = ConvertData.ConvertToDataTable<SYS_TournamentStepTable>(db.SYS_TournamentStepTables.Where(i => i.IsDeleted == false).ToList());
                }
            }
            catch (Exception objEx)
            {
                objResultMessage = new ResultMessage(true, ResultMessage.ErrorTypes.SearchData, "Lấy thông tin đặt bàn", objEx);
                DAL_Error.Insert(objResultMessage, 0, "DAL_TournamentStepTable --> LoadInfo");
                return objResultMessage;
            }

            return new ResultMessage(false, ResultMessage.ErrorTypes.No_Error, "Lấy danh sách đặt bàn thành công", string.Empty);
        }

        /// <summary>
        /// Lấy danh sách đặt bàn dạng LIST
        /// </summary>
        /// <param name="lstTournamentStepTable"></param>
        /// <returns></returns>
        public ResultMessage SelectToList(ref List<SYS_TournamentStepTable> lstTournamentStepTable)
        {
            ResultMessage objResultMessage = new ResultMessage();
            try
            {
                using (PokerLife db = new PokerLife())
                {
                    var TournamentStepTable = from b in db.SYS_TournamentStepTables
                                              where b.IsDeleted == false
                                              orderby b.OrderIndex
                                              select b;
                    lstTournamentStepTable = TournamentStepTable.ToList();
                }
            }
            catch (Exception objEx)
            {
                objResultMessage = new ResultMessage(true, ResultMessage.ErrorTypes.SelectData, "Lấy thông tin đặt bàn", objEx);
                DAL_Error.Insert(objResultMessage, 0, "DAL_TournamentStepTable --> LoadInfo");
                return objResultMessage;
            }

            return new ResultMessage(false, ResultMessage.ErrorTypes.No_Error, "Lấy danh sách đặt bàn thành công", string.Empty);
        }

        /// <summary>
        /// Lấy danh sách đặt bàn dạng List
        /// </summary>
        /// <param name="lstUserGroup">Đối tượng đặt bàn</param>
        /// <param name="intPageSize">Số lượng Record một trang</param>
        /// <param name="intPageNumber">Lấy trang số mấy</param>
        /// <returns></returns>
        public ResultMessage SelectToList(ref List<SYS_TournamentStepTable> lstTournamentStepTable, int intPageSize, int intPageNumber)
        {
            ResultMessage objResultMessage = new ResultMessage();
            try
            {
                using (PokerLife db = new PokerLife())
                {
                    var TournamentStepTable = from gu in db.SYS_TournamentStepTables
                                              where gu.IsDeleted == false
                                              orderby gu.OrderIndex
                                              select gu;

                    lstTournamentStepTable = TournamentStepTable.Skip((intPageNumber - 1) * intPageSize).Take(intPageSize).ToList();
                }
            }
            catch (Exception objEx)
            {
                objResultMessage = new ResultMessage(true, ResultMessage.ErrorTypes.SelectData, "Lấy thông tin đặt bàn", objEx);
                DAL_Error.Insert(objResultMessage, 0, "DAL_TournamentStepTable --> SelectToList");
                return objResultMessage;
            }

            return new ResultMessage(false, ResultMessage.ErrorTypes.No_Error, "Lấy danh sách đặt bàn thành công", string.Empty);
        }

        /// <summary>
        /// Lấy danh sách đặt bàn từ Cache
        /// </summary>
        /// <param name="lstTournamentStepTable"></param>
        /// <returns></returns>
        public ResultMessage SelectToListCache(ref List<SYS_TournamentStepTable> lstTournamentStepTable)
        {
            ResultMessage objResultMessage = new ResultMessage();
            //objResultMessage = DataCache.GetDataCache.GetTournamentStepTable(ref lstTournamentStepTable);
            return objResultMessage;
        }

        /// <summary>
        /// Lấy những thông tin đặt bàn để Cache
        /// </summary>
        /// <param name="lstTournamentStepTable"></param>
        /// <returns></returns>
        public ResultMessage SelectToCache(ref List<SYS_TournamentStepTable> lstTournamentStepTable,int intTournamentIDSys,Int64 intTournamentStepIDSys)
        {
            ResultMessage objResultMessage = new ResultMessage();
            try
            {
                Library.DataAccess.IData objIData = Library.DataAccess.Data.CreateData(DAL_Globals.ConnectString);
                objIData.ConnectNoEncrypt();
                SqlParameter[] para = new SqlParameter[2];
                para[0] = new SqlParameter("@TournamentIDSys", intTournamentIDSys);
                para[1] = new SqlParameter("@TournamentStepIDSys", intTournamentStepIDSys);

                objIData.CreateStoredProcedue("sp_SYS_TournamentStepTable_SRH");
                objIData.AddParameter(para);
                objIData.ExecProcedure();
            }
            catch (Exception objEx)
            {
                objResultMessage = new ResultMessage(true, ResultMessage.ErrorTypes.SelectData, "Lấy Cache danh sách đặt bàn", objEx);
                DAL_Error.Insert(objResultMessage, 0, "DAL_TournamentStepTable --> SelectToListCache");
                return objResultMessage;
            }

            return new ResultMessage(false, ResultMessage.ErrorTypes.No_Error, "Lấy Cache danh sách đặt bàn thành công", string.Empty);
        }

        #endregion
    }
}
